/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;

import com.bluestone.BaseAction;
import com.bluestone.assertor.IAssertor;
import com.bluestone.context.IContext;
import com.bluestone.scripts.ActionScript;
import com.bluestone.scripts.Function;
import com.bluestone.util.Util;

/**
 * CallFuntionAction can execute a function.
 * @author <a href="mailto:bluesotne.master@gmail.com">daniel.q</a>
 */
public class JDBCAction extends BaseAction{

	public JDBCAction(ActionScript action){
		super(action);
	}

	/**
	 * This action can execute a query.
	 * @return false 
	 */

	public boolean execute(IContext context){
		
		boolean result = false;
        try{
           
          
    	   String driver = (String)Util.getDynamicParaMap().get("jdbc.dirver");
    	   String url = (String)Util.getDynamicParaMap().get("jdbc.url");
    	   String user = (String)Util.getDynamicParaMap().get("jdbc.user");
    	   String password = (String)Util.getDynamicParaMap().get("jdbc.password");
    	   Util.getLogger().info("jdbc.driver:"+driver);
    	   Util.getLogger().info("jdbc.url:"+url);
    	   Util.getLogger().info("jdbc.user:"+user);
    	   Util.getLogger().info("jdbc.password:"+password);    
    	   Class.forName(driver).newInstance();
           if(url==null || url.length()==0 || user==null || user.length() ==0 ||password==null){
        	   Util.getLogger().error("jdbc config does not correct");
        	   return false;
           }
           
           String query = null;
           //query from file if target is not empty
           String target = action.getPara("target");
           if(target!=null && target.length()>0){
   			String file = Util.getString(target);
			String content = Util.loadFile(file);
			content = Util.getDynamicString(content);
			content = content.replaceAll("\r", " ");
			content = content.replaceAll("\n", " ");   
			query = content;
           }else{
        	   query = Util.getString(action.getPara("value"));
           }
           
           Util.getLogger().info("query:"+query);

 		   Connection   conn=   DriverManager.getConnection(url,user,password);
 		  
 		   int count = conn.createStatement().executeUpdate(query);

 		   Util.getLogger().info("update rows:"+count);
 		   conn.commit();
 		   conn.close(); 		   
 		   result = true;
        	
        }catch(Exception me){
        	me.printStackTrace();
        	Util.getLogger().error(me);        	
        }
		return result;		
	}
	




}


